package com.andatsoft.laisim.activity.setting;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.activity.BaseActivity;
import com.andatsoft.laisim.service.PlayerService;
import com.andatsoft.laisim.utils.Common;
import com.andatsoft.laisim.view.MyViewFlipper;
import com.andatsoft.laisim.view.SlimProgressBar;
import com.andatsoft.laisim.view.SlimProgressBar.SlimProgressClicked;

public class AudioSettingActivity extends BaseActivity {

	private CheckBox mChkAudioFade;
	private MyViewFlipper mViewFlipper;
	private Button mBtnOk;
	private SlimProgressBar mProgress;
	private TextView mTvFadeTimeValue, mTvFadeTime;

	private int mFadeTime = 1000;
	private int mTempFadeTime = 1000;

	// private CheckBox mChkBoxDownloadHighQuality;
	// private CheckBox mChkBoxDisplayDisplayAlbum;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_audio);
		initViews();
		setupListeners();
	}

	private void initViews() {
		// mChkBoxAutoDownload = (CheckBox)
		// findViewById(R.id.chk_box_album_art_auto_download);
		mChkAudioFade = (CheckBox) findViewById(R.id.chk_box_audio_fade);
		mViewFlipper = (MyViewFlipper) findViewById(R.id.view_fliper_setting_audio_fade_time);
		mBtnOk = (Button) findViewById(R.id.btn_ok_setting_audio_fade_time_value);
		mTvFadeTimeValue = (TextView) findViewById(R.id.tv_setting_audio_fade_time_value);
		mTvFadeTime = (TextView) findViewById(R.id.tv_setting_audio_fade_time);
		mProgress = (SlimProgressBar) findViewById(R.id.progress_setting_audio_fade_time);
		// mChkBoxDownloadHighQuality = (CheckBox)
		// findViewById(R.id.chk_box_album_art_download_high_quality);
		// mChkBoxDisplayDisplayAlbum = (CheckBox)
		// findViewById(R.id.chk_box_album_art_display_display);
		restoreUIFromSetting();
	}

	private void restoreUIFromSetting() {
		SharedPreferences sp = getSharedPreferences(
				Common.SP_AUDIO_SETTING_NAME, Context.MODE_PRIVATE);
		mFadeTime = sp.getInt(Common.SP_AUDIO_FADE_TIME, 1000);
		mTvFadeTimeValue.setText(mFadeTime + " ms");
		mTvFadeTime.setText(mFadeTime + " ms");
		mChkAudioFade.setChecked(sp.getBoolean(Common.SP_AUDIO_IS_FADE, true));
		updateLayoutWhenFadeChanged(mChkAudioFade.isChecked());
		mProgress.setMax(3000);
		mProgress.setProgress(mFadeTime);
	}

	private void updateLayoutWhenFadeChanged(boolean isCheck) {
		if (isCheck) {
			setAlpha(mViewFlipper, 1.0f);
			mViewFlipper.setEnabled(true);
		} else {
			setAlpha(mViewFlipper, 0.5f);
			mViewFlipper.setEnabled(false);
		}
	}

	private void setupListeners() {
		mBtnOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// save setting value
				SharedPreferences sp = getSharedPreferences(
						Common.SP_AUDIO_SETTING_NAME, Context.MODE_PRIVATE);
				SharedPreferences.Editor editor = sp.edit();
				editor.putInt(Common.SP_AUDIO_FADE_TIME, mFadeTime);
				editor.commit();
				mFadeTime = mTempFadeTime;
				mTvFadeTime.setText(mFadeTime + " ms");

				// Setup animation
				mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(
						getApplicationContext(),
						R.anim.slide_left_to_right_fade_in_fast));
				mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(
						getApplicationContext(),
						R.anim.slide_left_to_right_fade_out_fast));
				mViewFlipper.setDisplayedChild(0);
				sendBroadcast(new Intent(PlayerService.INTENT_SETTING_CHANGED));
			}
		});

		mChkAudioFade.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// save setting value
				SharedPreferences sp = getSharedPreferences(
						Common.SP_AUDIO_SETTING_NAME, Context.MODE_PRIVATE);
				SharedPreferences.Editor editor = sp.edit();
				editor.putBoolean(Common.SP_AUDIO_IS_FADE,
						mChkAudioFade.isChecked());
				editor.commit();

				if (mChkAudioFade.isChecked()) {
					// enable other options
					updateLayoutWhenFadeChanged(true);

				} else {
					// disable other options
					updateLayoutWhenFadeChanged(false);
					if (mViewFlipper.getDisplayedChild() == 0) {
						sendBroadcast(new Intent(
								PlayerService.INTENT_SETTING_CHANGED));
						return;
					}
					// Setup animation
					mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(
							getApplicationContext(),
							R.anim.slide_left_to_right_fade_in_fast));
					mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(
							getApplicationContext(),
							R.anim.slide_left_to_right_fade_out_fast));
					mViewFlipper.setDisplayedChild(0);
				}
				sendBroadcast(new Intent(PlayerService.INTENT_SETTING_CHANGED));
			}
		});

		mViewFlipper.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					if (mViewFlipper.getDisplayedChild() == 0)
						v.setBackgroundColor(0x50ffffff);
					return true;
				case MotionEvent.ACTION_UP:
					v.setBackgroundColor(0xff000000);
					if (mViewFlipper.getDisplayedChild() == 0) {

						// Setup animation
						mViewFlipper.setInAnimation(AnimationUtils
								.loadAnimation(getApplicationContext(),
										R.anim.slide_right_to_left_fade_in_fast));
						mViewFlipper.setOutAnimation(AnimationUtils
								.loadAnimation(
										getApplicationContext(),
										R.anim.slide_right_to_left_fade_out_fast));
						mViewFlipper.setDisplayedChild(1);
					}
					return true;
				case MotionEvent.ACTION_CANCEL:
					v.setBackgroundColor(0xff000000);
					return true;
				}
				return false;
			}
		});

		mProgress.setOnSlimClicked(new SlimProgressClicked() {

			@Override
			public void onClick(int newProgress) {
				mTempFadeTime = newProgress;
				mTvFadeTimeValue.setText(mTempFadeTime + " ms");
			}
		});

	}
}
